from abc import ABCMeta,abstractmethod

class Graphic(metaclass=ABCMeta):

    @abstractmethod
    def draw(self):
        pass


class Point(Graphic):
    def __init__(self,p1,p2):
        self.x = p1
        self.y = p2
    def __str__(self):
        return f"点（｛self.x｝,{self.y}）"

    def draw(self):
        print(str(self))

class Line(Graphic):

    def __init__(self,p1,p2):
        self.p1 = p1
        self.p2 = p2

    def __str__(self):
        return f"线【{self.p1},{self.p2}】"

    def draw(self):
        print(str(self))

class Picture(Graphic):
    def __init__(self,iterable):
        self.children = []
        for g in iterable:
            self.add(g)

    def add(self,graphic):
        self.children.append(graphic)
    def draw(self):
        print(" 复合图形")
        for g in  self.children:

            g.draw()
        print("复合图形绘制完")


p1 = Point(2,2)

l1 = Line(Point(10,20),Point(30,40))
l2 = Line(Point(50,60),Point(70,80))
pic1 = Picture([p1,l1,l2])

p1 = Point(6,6)

l1 = Line(Point(11,22),Point(31,41))
l2 = Line(Point(51,66),Point(71,91))

pic2=Picture([p1,l1,l2])

pic3 = Picture([pic1,pic2])
pic3.draw()

E:\source\python\code2pdfword\venv\Scripts\python.exe E:/source/python/code2pdfword/designpattern/dp/composite.py
 复合图形
 复合图形
点（｛self.x｝,2）
线【点（｛self.x｝,20）,点（｛self.x｝,40）】
线【点（｛self.x｝,60）,点（｛self.x｝,80）】
复合图形绘制完
 复合图形
点（｛self.x｝,6）
线【点（｛self.x｝,22）,点（｛self.x｝,41）】
线【点（｛self.x｝,66）,点（｛self.x｝,91）】
复合图形绘制完
复合图形绘制完